package com.lovepick;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.lovepick.http.HttpHelper;
import com.lovepick.util.BaseUtils;
import com.lovepick.util.Constant;

public class RegActivity extends Activity
{
	public static final String TAG = "RegActivity";
	private EditText edtUserName;
	private EditText edtPassword;
	private EditText edtPwdConfirm;
	private EditText edtEmail;
	private EditText edtTelephone;
	private EditText edtPayPwd;
	private EditText edtPayPwdConfirm;
	private ProgressDialog mDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reg);
		edtUserName = (EditText) this.findViewById(R.id.edt_nickname_reg);
		edtPassword = (EditText) this.findViewById(R.id.edt_password_reg);
		edtPwdConfirm = (EditText) this.findViewById(R.id.edt_pwdconfirm_reg);
		edtEmail = (EditText) this.findViewById(R.id.edt_email_reg);
		edtTelephone = (EditText) this.findViewById(R.id.edt_telephone_reg);
		edtPayPwd = (EditText) this.findViewById(R.id.edt_paypwd_reg);
		edtPayPwdConfirm = (EditText) this.findViewById(R.id.edt_paypwdconfirm_reg);
		mDialog = new ProgressDialog(this);

		edtUserName.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{

			@Override
			public void onFocusChange(View v, boolean hasFocus)
			{
				// TODO Auto-generated method stub
				if (!hasFocus)
				{
					if (!BaseUtils.isUsername(edtUserName.getText().toString()))
					{
						BaseUtils.showToast(RegActivity.this, R.string.username_error);
						edtUserName.setText("");
					}
				}
			}
		});

		edtEmail.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{

			@Override
			public void onFocusChange(View v, boolean hasFocus)
			{
				// TODO Auto-generated method stub
				if (!hasFocus)
				{
					if (!BaseUtils.isEmail(edtEmail.getText().toString()))
					{
						BaseUtils.showToast(RegActivity.this, R.string.email_error);
						edtEmail.setText("");
					}
				}

			}
		});

		edtPwdConfirm.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{

			@Override
			public void onFocusChange(View v, boolean hasFocus)
			{
				if (!hasFocus)
				{
					// TODO Auto-generated method stub
					String password = edtPassword.getText().toString();
					if (!password.equals(edtPwdConfirm.getText().toString()))
					{
						BaseUtils.showToast(RegActivity.this,
								R.string.password_not_match);
						edtPwdConfirm.setText("");
					}
				}
			}
		});

		edtPayPwdConfirm.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{

			@Override
			public void onFocusChange(View v, boolean hasFocus)
			{
				if (!hasFocus)
				{
					// TODO Auto-generated method stub
					String password = edtPayPwd.getText().toString();
					if (!password.equals(edtPayPwdConfirm.getText().toString()))
					{
						BaseUtils.showToast(RegActivity.this,
								R.string.pay_password_not_match);
						edtPayPwdConfirm.setText("");
					}
				}
			}
		});

		edtTelephone.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{

			@Override
			public void onFocusChange(View v, boolean hasFocus)
			{
				if (!hasFocus)
				{
					// TODO Auto-generated method stub
					if (!BaseUtils.isMobileNum(edtTelephone.getText()
							.toString()))
					{
						BaseUtils.showToast(RegActivity.this,
								R.string.telephone_error);
						edtTelephone.setText("");
					}
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case android.R.id.home:
				NavUtils.navigateUpFromSameTask(this);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void doLogin(View v)
	{
		Intent intent = new Intent(v.getContext(), LoginActivity.class);
		startActivity(intent);
		finish();
	}

	public void doReg(View v)
	{
		if (!TextUtils.isEmpty(edtUserName.getText().toString())
				&& !TextUtils.isEmpty(edtUserName.getText().toString())
				&& !TextUtils.isEmpty(edtPassword.getText().toString())
				&& !TextUtils.isEmpty(edtEmail.getText().toString())
				&& !TextUtils.isEmpty(edtTelephone.getText().toString())
				&& !TextUtils.isEmpty(edtPayPwd.getText().toString()))
		{
			new RegTask().execute();
		}
		else {
			BaseUtils.showToast(RegActivity.this, R.string.reg_info_not_finished);
		}
	}

	private class RegTask extends AsyncTask<Void, Void, String>
	{
		String username;
		String password;

		@Override
		protected void onPreExecute()
		{
			mDialog.setTitle(R.string.reg);
			mDialog.setMessage(getString(R.string.registering));
	    }

		@Override
		protected String doInBackground(Void... params)
		{
			// TODO Auto-generated method stub
			username = edtUserName.getText().toString();
			password = edtPassword.getText().toString();
			String email = edtEmail.getText().toString();
			String telephone = edtTelephone.getText().toString();
			String paypwd = edtPayPwd.getText().toString();

			List<NameValuePair> pairs = new ArrayList<NameValuePair>();
			pairs.add(new BasicNameValuePair("username", username));
			pairs.add(new BasicNameValuePair("password", password));
			pairs.add(new BasicNameValuePair("email", email));
			pairs.add(new BasicNameValuePair("telephone", telephone));
			pairs.add(new BasicNameValuePair("paypwd", paypwd));

			HttpHelper httpHelper = new HttpHelper();
			return httpHelper.doPost(Constant.HTTP_URL_REGISTER, pairs);
		}

		@Override
		protected void onPostExecute(String result)
		{
			mDialog.dismiss();

			try
			{
				result = result.trim();
				int resultCode = Integer.parseInt(result);
				Log.i(TAG, "resultCode = " + resultCode);
				switch (resultCode)
				{
					case 0:
						// 注册成功
						BaseUtils.showToast(RegActivity.this, R.string.register_success);
						Bundle data = new Bundle();
						data.putString("username", username);
						data.putString("password", password);
						Intent intent = new Intent(RegActivity.this, LoginActivity.class);
						intent.putExtra("login", data);
						startActivity(intent);
						finish();
						break;

					case 1:
						// 用户名与邮箱已被注册
						BaseUtils.showToast(RegActivity.this, R.string.username_email_not_unique);
						break;

					case 2:
						// 用户名与电话已被注册
						BaseUtils.showToast(RegActivity.this, R.string.username_telephone_not_unique);
						break;

					case 3:
						// 邮箱与电话号码已被注册
						BaseUtils.showToast(RegActivity.this, R.string.email_telephone_not_unique);
						break;

					case 4:
						// 用户名已被注册
						BaseUtils.showToast(RegActivity.this, R.string.username_not_unique);
						break;

					case 5:
						// 邮箱已被注册
						BaseUtils.showToast(RegActivity.this, R.string.email_not_unique);
						break;

					case 6:
						// 电话号码已被注册
						BaseUtils.showToast(RegActivity.this, R.string.telephone_not_unique);
						break;

					case 7:
						// 用户名、邮箱与电话号码均已被注册
						BaseUtils.showToast(RegActivity.this, R.string.username_email_telephone_not_unique);
						break;

					default:
						BaseUtils.showToast(RegActivity.this, R.string.register_fail);
						break;
				}
			}
			catch (NumberFormatException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				BaseUtils.showToast(RegActivity.this, R.string.network_connnect_fail);
			}
	    }

	}

}
